package com.ccjk.beusoft.hot

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.Build
import android.os.Bundle
import android.os.IBinder
import android.support.design.widget.FloatingActionButton
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
import android.util.Log
import android.view.Choreographer
import android.view.Menu
import android.view.MenuItem
import android.view.View
import java.io.*

class MainActivity : AppCompatActivity() {
    private lateinit var apkFile: File


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        BinderHookHelper.hookClipboardService()

        setContentView(R.layout.activity_main)
        val toolbar = findViewById<View>(R.id.toolbar) as Toolbar
        setSupportActionBar(toolbar)
//        testLaunchMode()

//        testChoreographer();

//        testServers()

        Runtime.getRuntime().addShutdownHook(Thread(Runnable {

        }))



    }


    private val connection = object : ServiceConnection {
        override fun onServiceDisconnected(name: ComponentName?) {
        }

        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {

            val serviceConnection = IBinderPool.Stub.asInterface(service);
        }

    };

    fun testServers() {

        bindService(Intent(this@MainActivity, BinderPoolService::class.java), connection, Context.BIND_AUTO_CREATE)
    }


    private var lastTime = 0L;
    private val callback2: Choreographer.FrameCallback = object : Choreographer.FrameCallback {
        override fun doFrame(frameTimeNanos: Long) {// 1/60=16.6

            Log.e("time", "" + (frameTimeNanos - lastTime) / 1000);
            lastTime = frameTimeNanos;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                Choreographer.getInstance().postFrameCallback(this)
            }
        }
    }


    fun testChoreographer() {
//        Log.e("time", "" + frameTimeNanos);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            Choreographer.getInstance().postFrameCallback(callback2)
        }
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
    }

    override fun onPause() {
        super.onPause()
        Log.e("mainactivity", "onPause")
    }

    override fun onSaveInstanceState(outState: Bundle?) {
        super.onSaveInstanceState(outState)
        Log.e("mainactivity", "onSaveInstanceState")
    }

    override fun onStop() {
        super.onStop()
        Log.e("mainactivity", "onStop")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.e("mainactivity", "onDestroy")
        unbindService(connection);
    }

    private fun testLaunchMode() {
        val fab = findViewById<View>(R.id.fab) as FloatingActionButton
        fab.setOnClickListener {
            startActivity(Intent(this@MainActivity, TwoActivity::class.java))
        }

    }


//    private fun testPlugin() {
//        apkFile = File(cacheDir, "plugin/plugin.apk")
//        apkFile.parentFile.mkdir()
//
//
//        val fab = findViewById<View>(R.id.fab) as FloatingActionButton
//        fab.setOnClickListener {
//
//
//            Thread {
//                IoUtil.copyFile(resources.assets.open("plugin-debug.apk"), apkFile)
//                runOnUiThread {
//                    PluginManager.getInstance(this).loadApk(apkFile.absolutePath)
//                    Toast.makeText(this, "加載插件成功", Toast.LENGTH_SHORT).show()
//                }
//            }.start()
//
//        }
//        findViewById<TextView>(R.id.tv_start).setOnClickListener {
//            //           MyManager().test()
//            PluginManager.getInstance(this).startActivity(this, PluginIntent("beusoft.ccjk.com.plugin"))
//        }
//
//        val largeImageview = findViewById<LargeImageView>(R.id.iv_large_view);
//        largeImageview.setInputStream(assets.open("qm.jpg"));
//    }

    private fun getCpuName(): String? {
        try {
            val fr = FileReader("/proc/cpuinfo")
            val br = BufferedReader(fr as Reader)
            val text = br.readLine()
            br.close()
            val array = text.split(":\\s+".toRegex(), 2).toTypedArray()
            if (array.size >= 2) {
                return array[1]
            }
        } catch (e: FileNotFoundException) {
            e.printStackTrace()
        } catch (e: IOException) {
            e.printStackTrace()
        }

        return null
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.menu_main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        val id = item.itemId


        return if (id == R.id.action_settings) {
            true
        } else super.onOptionsItemSelected(item)

    }
}
